package com.apes.framework.plugin.workflow.domain;

import com.alibaba.fastjson.JSONObject;
import com.apes.framework.api.DomainService;
import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.logger.ApesLogger;
import com.apes.framework.jpa.logger.ApesLoggerRepository;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.plugin.event.Listener;
import com.apes.scm.masterdata.frame.model.Branch;
import com.apes.scm.masterdata.frame.repository.BranchRepository;
import com.apes.scm.rbac.model.User;
import com.apes.scm.rbac.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


/**
 * 功能：ProcessInstanceExtRepository 服务
 *
 * @author xul
 * @create 2018-03-13 19:13
 */
@Service
public class ProcessInstanceExtService extends DomainService {


    @Autowired
    ProcessInstanceExtRepository repository;
    @Autowired
    UserRepository userRepository;
    @Autowired
    BranchRepository branchRepository;

    /**
     * 功能：创建ProcessInstanceExt
     */
    @Listener(topic = "event:wf.processInstanceExt.create")
    public ProcessInstanceExt save(SimpleRequest request) {
        ProcessInstanceExt processInstanceExt = createDate(request);
        return saveAndFlush(processInstanceExt);
    }

    public ProcessInstanceExt saveAndFlush(ProcessInstanceExt processInstanceExt) {
        return repository.saveAndFlush(processInstanceExt);
    }

    public ProcessInstanceExt findByProcessInstanceId(String processInstanceId) {
        return repository.findFirstByProcessInstanceId(processInstanceId);
    }

    public ProcessInstanceExt findByBusinessTypeAndBusinessKey(String businessType, String businessKey) {
        return repository.findFirstByBusinessTypeAndBusinessKeyOrderByIdDesc(businessType, businessKey);
    }

    /***
     * 构建结构
     * 1. 存在发起人为空的情况
     * 2. 存在发起人传入值为partyRole的情况（未知）
     */
    private ProcessInstanceExt createDate(SimpleRequest request){
        JSONObject tempJson = request.getJO();
        ProcessInstanceExt processInstanceExt = new ProcessInstanceExt();

        processInstanceExt.setBusinessKey(tempJson.getString("businessKey"));
        processInstanceExt.setBusinessType(tempJson.getString("businessType"));
        processInstanceExt.setProcessInstanceId(tempJson.getString("processInstanceId"));

        // 获取User
        User user = null;
        if (tempJson.get("processInitiator") != null && ( tempJson.get("processInitiator") instanceof String ||
                tempJson.get("processInitiator") instanceof Integer)) {

            user = userRepository.findOne( tempJson.get("processInitiator") instanceof String ?
                    Integer.valueOf(tempJson.getString("processInitiator")) :
                    tempJson.getInteger("processInitiator"));
        }
        // 获取Branch
        Branch branch = null;
        if (tempJson.get("processInitiatingDepartment") != null && tempJson.get("processInitiatingDepartment") instanceof String) {
            branch = branchRepository.findOne( tempJson.getString("processInitiatingDepartment"));
        }

        if (user != null) processInstanceExt.setProcessInitiator(user);
        if (branch != null) processInstanceExt.setProcessInitiatingDepartment(branch);

        return processInstanceExt;
    }
}
